//
//  SetViewController.m
//  IntelligentHousehold
//
//  Created by wmwMAC on 17/6/26.
//  Copyright © 2017年 Cassie. All rights reserved.
//

#import "SetViewController.h"

#import "LockSetTableViewCell.h"

#import "LockSetViewController.h"//开锁设置
#import "AboutUsViewController.h"//关于我们
#import "GuideViewController.h"//欢迎页
#import "ChangePasswdViewController.h"//修改密码
#import "LogInViewController.h"//退出登录


static NSString *identifier = @"LockSetTableViewCell";
@interface SetViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSArray *cellNameArr;
    NSArray *cellImgArr;
}
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation SetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = BaseColor;
    self.navigationItem.title = @"设置";
    
    cellNameArr = @[@"开锁设置",@"关于我们",@"修改密码",@"欢迎页"];
    cellImgArr = @[@"kssz_szmshm",@"sz_gywm",@"sz_xgmm",@"sz_hyy"];
    
    [self addTableviewUI];
    
    
    
    

    
}
- (void)addTableviewUI {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = BaseColor;
    [self.tableView registerNib:[UINib nibWithNibName:@"LockSetTableViewCell" bundle:nil] forCellReuseIdentifier:identifier];
    [self.view addSubview:self.tableView];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else{
        return 3;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LockSetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    cell.titleImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",cellImgArr[indexPath.row + indexPath.section*1]]];
    cell.titleLabel.text = cellNameArr[indexPath.row + indexPath.section*1];
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        
        return 10;
    }else {
        return 0.01;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = BaseColor;
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    if (section == 1) {
        return 150;
    }
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footview = [[UIView alloc] init];
    footview.backgroundColor = BaseColor;
    
    if (section == 1) {
        CGFloat footBtnHeight = 50;
        UIButton *footBtn = [MyUtil createBtnFrame:CGRectMake(10, 50, kUIScreenWidth-20, footBtnHeight) title:@"退出登录" bgImage:nil selectBgImage:nil image:nil target:self action:@selector(LogOutAction)];
        footBtn.backgroundColor = kRGB(251, 76, 82);
        footBtn.layer.cornerRadius = 5;
        footBtn.clipsToBounds = YES;
        [footBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [footview addSubview:footBtn];
        
    }
    
    return footview;
}
#pragma mark -------------- 退出登录 --------
-(void)LogOutAction{
    NSLog(@"LogOutAction");
    //清空数据
    
    [SaveDataMethord cleanloginData];
    
    LogInViewController *loginVC = [[LogInViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVC];
    self.view.window.rootViewController = nav;
    
    
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        
            LockSetViewController *lockSetVC = [[LockSetViewController alloc] init];
            [self.navigationController pushViewController:lockSetVC animated:YES];
        
        
        
    }else {
        if (indexPath.row == 0) {
            AboutUsViewController *aboutUsVC = [[AboutUsViewController alloc] init];
            [self.navigationController pushViewController:aboutUsVC animated:YES];
        }else if (indexPath.row == 2){
            
            [self.navigationController pushViewController:[[GuideViewController alloc] init] animated:YES];
        
        }else{
            ChangePasswdViewController *changePasswdVC = [[ChangePasswdViewController alloc] init];
            [self.navigationController pushViewController:changePasswdVC animated:YES];
        }
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
